<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="1"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveHandle" type="primary">保 存</el-button>
      <el-button @click="closeDialog('closeDialog')">返 回</el-button>
    </div>
  </div>
</template>

<script>
import infoModel from './infoModel';
import { postSave, getAreaData } from '@/api/serviceass/pdimanage/area'; 
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'servicessbpdimanageareainfo',
  mixins: [dictionary],
  props:{
    value: {
      type: Object,
      data: Object,
      required: true
    },
  },
  data() {
    this.saveData = null;
    return {
      type: this.value.type,
      formList: this.createFormList(),
      loadingFlag: false,
      id: '',
      areasList: []
    }
  },
  created() {
    this.init(this.value.data)
  },
  methods: {
    init(data) {
      if(this.value.type==='edit'){
        this.id = data.id
        this.formList.forEach(x => {
          x.initialValue = data[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '区域描述',
          fieldName: 'fwqymc',
          placeholder: '请输入',
          initialValue: '',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'INPUT',
          label: '优先级',
          fieldName: 'fwyxjb',
          placeholder: '请输入',
          initialValue: '',
          rules: [{
            required: true,
            message: '必填',
            trigger: 'blur'
          }]
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'xtqybz',
          placeholder: '请选择',
          initialValue: "",
          itemList: this.createDictList('1003'),
          rules: [{
            required: true,
            message: '必填',
            trigger: 'change'
          }]
        },
      ]
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
      //保存
    async saveHandle(val) {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData) {
        this.saveData.type = this.type
        this.saveData.id = this.id
        this.loadingFlag = true;
        var data = await getAreaData({ "fwqymc": this.saveData.fwqymc, "isId" : this.saveData.id })
        if (data.resultCode === 200) {
          this.areasList = data.data.records
          if (this.areasList !== null && this.areasList !== undefined && this.areasList !== [] && this.areasList.length > 0) {
            this.loadingFlag = false;
            this.$notify.success({title: '提示信息',message:'保存失败，已存在的区域!'});
          } else {
            let res = await postSave(this.saveData)
            console.info(res)
            if (res.resultCode === 200) {
              this.$notify.success({title: '提示信息',message:'保存成功!'});
              this.loadingFlag = false;
              this.closeDialog('refresh')
            } else {
              this.loadingFlag = false;
              this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
            }
          }
        }
      }
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', {visible: false, type: type});
    }
  }
}
</script>